package com.jianan.mis.app.basedata;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import com.jianan.mis.data.domain.Group;
import com.jianan.mis.data.domain.View;
import com.jianan.mis.service.GroupService;
import com.jianan.mis.service.ViewService;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.sys.SpringBeanFactory;

@SuppressWarnings("unchecked")
public class GroupView extends ViewPart {

	class ListLabelProvider_1 extends LabelProvider {
		public String getText(Object element) {
			return (String) element;
		}
		public Image getImage(Object element) {
			return null;
		}
	}
	class ContentProvider_1 implements IStructuredContentProvider {
		public Object[] getElements(Object inputElement) {
			return ((List) inputElement).toArray();
		}
		public void dispose() {
		}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}
	private Combo combo;
	class ListLabelProvider extends LabelProvider {
		public String getText(Object element) {
			View view = (View) element;
			return view.getName();
		}
		public Image getImage(Object element) {
			return null;
		}
	}
	class ContentProvider implements IStructuredContentProvider {
		public Object[] getElements(Object inputElement) {
			return ((Set) inputElement).toArray();
		}
		public void dispose() {
		}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}
	class ViewSorter extends ViewerSorter {
		public int compare(Viewer viewer, Object e1, Object e2) {
			View view1 = (View) e1;
			View view2 = (View) e2;
			return view1.getId().compareTo(view2.getId());
		}
	}
	private org.eclipse.swt.widgets.List selectedList;
	private org.eclipse.swt.widgets.List allList;
	private ListViewer selectedListViewer;
	private Text txtName;
	private Text txtCode;
	public static final String ID = "com.jianan.mis.app.basedata.GroupView"; //$NON-NLS-1$
	
	private ViewService viewService;
	
	private GroupService groupService;
	
	private Group currentGroup;

	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		
		if(!initializeServices()) {
			MessageDialog.openError(getViewSite().getShell(),
					IMessageKeys.OPEN_VIEW_FAIL, IMessageKeys.SERVICE_UNAVAILABLE);
			return;
		}
		
		Composite container = new Composite(parent, SWT.NONE);

		final Label label = new Label(container, SWT.NONE);
		label.setText("选择组别:");
		label.setBounds(60, 25, 67, 12);

		final ComboViewer groupsComboViewer = new ComboViewer(container, SWT.READ_ONLY);
		groupsComboViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(final SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				currentGroup = groupService.findByName((String) selection.getFirstElement());
				txtCode.setText(currentGroup.getCode());
				txtName.setText(currentGroup.getName());
				selectedListViewer.setInput(currentGroup.getViews());
			}
		});
		groupsComboViewer.setLabelProvider(new ListLabelProvider_1());
		groupsComboViewer.setContentProvider(new ContentProvider_1());
		combo = groupsComboViewer.getCombo();
		combo.setBounds(140, 20, 106, 20);
		List<Group> groups = groupService.getAll();
		List<String> groupNames = new ArrayList<String>();
		for(Group group : groups) {
			groupNames.add(group.getName());
		}
		groupsComboViewer.setInput(groupNames);

		final org.eclipse.swt.widgets.Group group = new org.eclipse.swt.widgets.Group(container, SWT.NONE);
		group.setText("组别信息");
		group.setBounds(50, 60, 568, 370);

		final Label label_1 = new Label(group, SWT.NONE);
		label_1.setText("组别代码:");
		label_1.setBounds(26, 32, 62, 12);

		final Label label_2 = new Label(group, SWT.NONE);
		label_2.setText("组别名称:");
		label_2.setBounds(276, 32, 62, 12);

		txtCode = new Text(group, SWT.READ_ONLY | SWT.BORDER);
		txtCode.setBounds(90, 25, 149, 25);

		txtName = new Text(group, SWT.READ_ONLY | SWT.BORDER);
		txtName.setBounds(340, 25, 149, 25);

		final Label label_3 = new Label(group, SWT.NONE);
		label_3.setText("全部视图:");
		label_3.setBounds(140, 80, 62, 12);

		final ListViewer allListViewer = new ListViewer(group, SWT.V_SCROLL | SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
		allListViewer.setSorter(new ViewSorter());
		allListViewer.setLabelProvider(new ListLabelProvider());
		allListViewer.setContentProvider(new ContentProvider());
		allList = allListViewer.getList();
		allList.setBounds(95, 100, 140, 239);
		Set<View> set = new HashSet<View>(viewService.getAll());
		allListViewer.setInput(set);

		selectedListViewer = new ListViewer(group, SWT.V_SCROLL | SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
		selectedListViewer.setSorter(new ViewSorter());
		selectedListViewer.setLabelProvider(new ListLabelProvider());
		selectedListViewer.setContentProvider(new ContentProvider());
		selectedList = selectedListViewer.getList();
		selectedList.setBounds(320, 100, 140, 239);

		final Button btnAdd = new Button(group, SWT.NONE);
		btnAdd.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if(currentGroup == null) {
					MessageDialog.openInformation(e.display.getActiveShell(), 
							IMessageKeys.FAIL, "请选择一个组别!");
					return;
				}
				// 取得选择要添加的view
				IStructuredSelection selections = (IStructuredSelection) allListViewer.getSelection();
				Iterator iterator = selections.iterator();
				Set<View> selectedViews = (Set<View>) selectedListViewer.getInput();
				while(iterator.hasNext()) {
					View view = (View) iterator.next();
					selectedViews.add(view);
				}
				selectedListViewer.setInput(selectedViews);
			}
		});
		btnAdd.setText("添加");
		btnAdd.setBounds(255, 165, 48, 22);

		final Label label_3_1 = new Label(group, SWT.NONE);
		label_3_1.setBounds(358, 80, 66, 12);
		label_3_1.setText("可访问视图:");

		final Button btnDel = new Button(group, SWT.NONE);
		btnDel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if(currentGroup == null) {
					MessageDialog.openInformation(e.display.getActiveShell(), 
							IMessageKeys.FAIL, "请选择一个组别!");
					return;
				}
				// 取得选择要移除的view
				IStructuredSelection selections = (IStructuredSelection) selectedListViewer.getSelection();
				Iterator iterator = selections.iterator();
				Set<View> selectedViews = (Set<View>) selectedListViewer.getInput();
				while(iterator.hasNext()) {
					View view = (View) iterator.next();
					selectedViews.remove(view);
				}
				selectedListViewer.setInput(selectedViews);
			}
		});
		btnDel.setText("移除");
		btnDel.setBounds(255, 220, 48, 22);

		final Button btnUpdate = new Button(group, SWT.NONE);
		btnUpdate.addSelectionListener(new SelectionAdapter() {	
			public void widgetSelected(final SelectionEvent e) {
				if(currentGroup == null) {
					MessageDialog.openInformation(e.display.getActiveShell(), 
							IMessageKeys.FAIL, "请选择一个组别!");
					return;
				}
				currentGroup.setViews((Set) selectedListViewer.getInput());
				try {
					groupService.update(currentGroup);
					getViewSite().getActionBars().getStatusLineManager()
						.setMessage("组别信息已保存.");
					
					allListViewer.setSelection(null);
					selectedListViewer.setSelection(null);
					
				} catch (Exception ex) {
					MessageDialog.openError(e.display.getActiveShell(),
							IMessageKeys.FAIL, ex.getMessage());
					return;
				}
			}
		});
		btnUpdate.setText("更新组别");
		btnUpdate.setBounds(475, 310, 62, 25);
		//
		createActions();
		initializeToolBar();
		initializeMenu();
	}
	
	private boolean initializeServices() {
		viewService = (ViewService) SpringBeanFactory.getBean("viewService");
		if(viewService == null) {
			return false;
		}
		groupService = (GroupService) SpringBeanFactory.getBean("groupService");
		if(groupService == null) {
			return false;
		}
		return true;
	}

	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

}
